﻿using Microsoft.AspNetCore.Authentication.Cookies;
using System.Security.Claims;

namespace Dotnet6.Web.ServiceExtensions
{
    public static class CookieServiceExtensions
    {
        public static void AddCoreCookies(this IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(opt => 
                {
                    opt.ExpireTimeSpan = TimeSpan.FromMinutes(5);
                    opt.SlidingExpiration = true;
                    opt.LoginPath = "/auth/login";
                });

            services.AddAuthorization(config =>
            {
                //第一种：通过简单方法增加授权策略
                config.AddPolicy("ManagePolicy", p => p.RequireClaim(ClaimTypes.Role, "admin"));
            });
        }
    }
}
